package com.tang.bloghandle.controller;


import cn.hutool.core.exceptions.ExceptionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.bloghandle.dto.BlogInfoDto;
import com.tang.bloghandle.dto.PageBlogDto;
import com.tang.bloghandle.entity.BlogEntity;
import com.tang.bloghandle.service.IBlogService;
import com.tang.bloghandle.vo.BlogInfoVo;
import com.tang.common.utils.UserUtils;
import com.tang.common.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 博客 前端控制器
 * </p>
 *
 * @author tang.shencai
 * @since 2024-08-17
 */
@Api(tags = "博客")
@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {
    @Autowired
    private IBlogService blogService;

    @ApiOperation("分页查询博客目录")
    @PostMapping("/pageCatalog")
    public R<Page<BlogInfoVo>> pageCatalog(@RequestBody PageBlogDto dto){
        try {
            return R.success(blogService.pageCatalog(dto));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询我的博客目录")
    @PostMapping("/pageMyCatalog")
    public R<Page<BlogEntity>> pageMyCatalog(@RequestBody PageBlogDto dto){
        try {
            return R.success(blogService.pageMyCatalog(dto));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("分页查询我的回收站目录")
    @PostMapping("/pageMyBin")
    public R<Page<BlogEntity>> pageMyBin(@RequestBody PageBlogDto dto){
        try {
            return R.success(blogService.pageMyBin(dto));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("查询博客")
    @GetMapping("/get/{id}")
    public R<String> get(@PathVariable("id") Integer id){
        try {
            return R.success(blogService.getById(id).getContent());
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("全局替换")
    @GetMapping("/replace")
    public R replace(@RequestParam String oldStr,@RequestParam String newStr){
        try {
            blogService.replace(oldStr,newStr);
            return R.success();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("查询置顶博客")
    @GetMapping("/getTopBlog")
    public R<List<BlogEntity>> getTopBlog(){
        try {
            return R.success(blogService.getTopBlog());
        }catch (Exception e){
            log.error(ExceptionUtil.getMessage(e));
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("查询当前处理人博客信息")
    @GetMapping("/getCurrentUserBlogInfo")
    public R<BlogInfoDto> getCurrentUserBlogInfo(){
        try {
            return R.success(blogService.getCurrentUserBlogInfo());
        }catch (Exception e){
            log.error(ExceptionUtil.getMessage(e));
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("修改博客信息")
    @PostMapping("/update")
    public R updateBlog(@RequestBody BlogEntity entity){
        try {
            blogService.updateBlog(entity);
            return R.success();
        }catch (Exception e){
            log.error(ExceptionUtil.getMessage(e));
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("删除博客")
    @GetMapping("/delete")
    public R deleteBlog(@RequestParam Integer id){
        try {
            blogService.deleteBlog(id);
            return R.success();
        }catch (Exception e){
            log.error(ExceptionUtil.getMessage(e));
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("新增博客")
    @PostMapping("/add")
    public R addBlog(@RequestBody BlogEntity entity){
        try {
            entity.setWriter(UserUtils.getCurrentUserName());
            entity.setCreateTime(new Date());
            blogService.save(entity);
            return R.success();
        }catch (Exception e){
            log.error(ExceptionUtil.getMessage(e));
            return R.error(e.getMessage());
        }
    }
}
